Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmORD120
		Inherits System.Windows.Forms.Form
		Dim OldSTIDN As String
		Private Sub WriteToFile()
			Dim SQLInsert As String
			If ISADDING Then
				SQLInsert = " INSERT INTO EST " & "(STCNO,STSTN,STCNM,STSCN,STSAD,STTEL) " & "VALUES " & "('" & txtSTCNO.Text & "', '" & txtSTSTN.Text & "','" & txtSTCNM.Text & "', '" & txtSTSCN.Text & "','" & txtSTSAD.Text & "', '" & txtSTTEL.Text & "')"
			Else
				SQLInsert = "UPDATE EST SET STCNO = '" & txtSTCNO.Text & "', STSTN ='" & txtSTSTN.Text & "' " & ",STCNM='" & txtSTCNM.Text & "',STSCN= '" & txtSTSCN.Text & "'" & ",STSAD='" & txtSTSAD.Text & "', STTEL='" & txtSTTEL.Text & "'" & " WHERE STCNO + ' - ' + STSTN='" & OldSTIDN & "'"
			End If

			DBconn.Execute(SQLInsert)
			Adodc1.Refresh()

		End Sub

		Private Sub WriteToScreen()
			Dim TBL As ADODB.Recordset
			If ISADDING Then 'gan rong
				txtSTCNO.Text = ""
				txtSTSTN.Text = ""
				txtCMNME.Text = ""
				txtSTCNM.Text = ""
				txtSTSCN.Text = ""
				txtSTSAD.Text = ""
				txtSTTEL.Text = ""
			Else
				TBL = DBconn.Execute("  Select EST.*From EST where  STCNO ='" & txtSTCNO.Text & "' and STSTN='" & txtSTSTN.Text & "'")
				If TBL.EOF = False Then
					txtSTCNO.Text = IIf(IsDBNull(TBL.Fields("STCNO").Value), "", TBL.Fields("STCNO").Value)
					txtCMNME.Text = Find("RCM", "CMNME", " WHERE CMCNO='" & txtSTCNO.Text & "'")
					txtSTSTN.Text = IIf(IsDBNull(TBL.Fields("STSTN").Value), "", TBL.Fields("STSTN").Value)
					txtSTCNM.Text = IIf(IsDBNull(TBL.Fields("STCNM").Value), "", TBL.Fields("STCNM").Value)
					txtSTSCN.Text = IIf(IsDBNull(TBL.Fields("STSCN").Value), "", TBL.Fields("STSCN").Value)
					txtSTSAD.Text = IIf(IsDBNull(TBL.Fields("STSAD").Value), "", TBL.Fields("STSAD").Value)
					txtSTTEL.Text = IIf(IsDBNull(TBL.Fields("STTEL").Value), "", TBL.Fields("STTEL").Value)
				Else
					cboSTCNO.BoundText = ""
					txtSTCNO.Text = ""
					txtSTSTN.Text = ""
					txtCMNME.Text = ""
					txtSTCNM.Text = ""
					txtSTSCN.Text = ""
					txtSTSAD.Text = ""
					txtSTTEL.Text = ""
				End If
				TBL.Close()
			End If
			OldSTIDN = txtSTCNO.Text & " - " & txtSTSTN.Text
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			Frame1.Enabled = ISEDITING

		End Sub

		Private Sub cboSTCNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboSTCNO.ClickEvent
			If eventArgs.area = 2 Then
				txtSTCNO.Text = cboSTCNO.BoundText
				txtCMNME.Text = cboSTCNO.CtlText
				cboSTCNO.Visible = False
				txtSTSTN.Focus()
			End If


		End Sub

		Private Sub cboSTIDN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboSTIDN.ClickEvent
			If eventArgs.area = 2 Then
				txtSTCNO.Text = Find("EST", "STCNO", " WHERE STCNO + ' - ' + STSTN='" & cboSTIDN.BoundText & "'")
				txtSTSTN.Text = Find("EST", "STSTN", " WHERE STCNO + ' - ' + STSTN='" & cboSTIDN.BoundText & "'")
				WriteToScreen()
			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen()

			' Ham Gan tri man hinh
			ButtonStatus()
			txtSTCNO.Focus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False

			ButtonStatus()
			txtSTCNO.Text = cboSTCNO.BoundText
			WriteToScreen()


		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			Dim Response As Object
			Dim SQL As String
			Response = MsgBox("Do you really to delte this Sale man?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.DefaultButton2, "Chu y'")
			If Response = MsgBoxResult.Yes Then
				SQL = "DELETE FROM EST WHERE STCNO = '" & cboSTCNO.BoundText & "'"

				cmdExecute = New ADODB.Command
				cmdExecute.ActiveConnection = DBconn
				cmdExecute.CommandText = SQL
				cmdExecute.Execute()
				Adodc1.Refresh()
			End If
			'WriteToScreen (0)

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True


			ButtonStatus()

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click


			WriteToFile()
			Adodc1.Refresh()

			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()


		End Sub

		Private Sub frmORD120_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			'BIENCHUNG
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "Select  STCNO + ' - ' + STSTN as STIDN  from EST"
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "Select * from RCM WHERE CMRID<>'RZ'"
			Adodc2.Refresh()
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			VB6_AddADODataBinding()
			Me.cboSTIDN.RowSource = ADOBind_Adodc1.DataSource
			Me.cboSTCNO.RowSource = ADOBind_Adodc2.DataSource
		End Sub



		Private Sub txtSTCNM_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSTCNM.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtSTSCN.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtSTCNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSTCNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtSTCNO.Text & "'")
				If Result = "" Then
					cboSTCNO.Visible = True
					cboSTCNO.Focus()
				Else
					cboSTCNO.Visible = False
					txtCMNME.Text = Result
					txtSTSTN.Focus()
				End If
			End If


			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtSTSAD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSTSAD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtSTTEL.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtSTSCN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSTSCN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtSTSAD.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtSTSTN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSTSTN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim OldSTCNO As Object
			If KeyAscii = 13 Then
				If ValidMaso(OldSTCNO, txtSTCNO.Text & " - " & txtSTSTN.Text, "EST", " STCNO='" & txtSTCNO.Text & "' AND STSTN='" & txtSTSTN.Text & "'") Then
					MsgBox("This code is already exist")
					txtSTCNO.Focus()
				Else
					txtSTCNM.Focus()
				End If
			End If

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtSTTEL_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSTTEL.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then cmdSave.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace